﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using TestSapco;

namespace SRM
{
    public partial class TestEmail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void SendTestEmail_Button_Click(object sender, EventArgs e)
        {
            Help.SendEmail("zhixian.ong@keppelland.com", "test e-mail", "this is a test e-mail", false);
        }

        protected void Sing_Button_Click(object sender, EventArgs e)
        {
            //DateTime dtDueDate = DateTime.ParseExact("08:30", "HH:mm", CultureInfo.InvariantCulture);

            //if (DateTime.Now > dtDueDate)
            //{
            //    Help.SendEmail("zhixian.ong@keppelland.com", "overdue sync", "overdue sync", false);
            //}

            SAPB2E sap = new SAPB2E();

            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SRM"].ConnectionString))
            {
                System.Data.DataSet ds = new DataSet();
                System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
                System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter(cmd);
                cmd.CommandText = "SELECT * FROM SAP_CodeType WHERE SapFunctionName IS NOT NULL";
                da.Fill(ds);
                DataTable dtCodeTypes = ds.Tables[0];

                conn.Open();
                foreach (DataRow row in dtCodeTypes.Rows)
                {
                    DataTable sapDataTable = sap.GetDataTableFromFunction(
                        row.Field<string>("SapFunctionName"),
                        row.Field<string>("Id"),
                        row.Field<bool>("HasParentId1"),
                        row.Field<bool>("HasParentId2"));

                    using (System.Data.SqlClient.SqlTransaction sqlTran = conn.BeginTransaction())
                    {
                        cmd.CommandText = string.Format("DELETE FROM SAP_Code WHERE CodeTypeId = '{0}'", row["Id"].ToString());
                        cmd.Transaction = sqlTran;
                        cmd.ExecuteNonQuery();

                        using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(conn, System.Data.SqlClient.SqlBulkCopyOptions.Default, sqlTran))
                        {
                            bcp.DestinationTableName = "SAP_Code";
                            bcp.WriteToServer(sapDataTable);
                        }

                        sqlTran.Commit();

                    } // using (System.Data.SqlClient.SqlTransaction sqlTran = conn.BeginTransaction())
                } // foreach (DataRow row in dtCodeTypes.Rows)
            } // using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SRM"].ConnectionString))

        }
    }

} // namespace SRM